
package com.greensecurity.sim.controller;

import com.greensecurity.sim.entity.SwiperItem;
import com.greensecurity.sim.repository.SwiperItemRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @author :admin
 * @date :2021/8/31 11:08:06
 * @description:
 */
@Controller
@RequestMapping
@Slf4j
@Api(description = "默认管理")
public class IndexController {
    @Resource
    private SwiperItemRepository swiperItemRepository;


    @Value("${system.usermanage.loginUrl}")
    private String loginUrl="/";

    @ApiOperation(value = "默认")
    @GetMapping("/")
    public String index(){
        return "index";
    }

    @ApiOperation(value = "清除session")
    @GetMapping("/out")
    public String out(HttpServletRequest request){
        request.getSession().setAttribute("user",null);
        return  "redirect:"+loginUrl;
    }

    @ApiOperation(value = "轮播图")
    @ResponseBody
    @GetMapping("/getSwiperItems/{Id}")
    public Optional<SwiperItem> swiperItems(@PathVariable Long Id){
        return swiperItemRepository.findById(Id);
    }

    @ApiOperation(value = "增加轮播图")
    @ResponseBody
    @PostMapping("/setSwiperItem")
    public String setSwiperItem(SwiperItem swiperItem){
        try {
            Date date = new Date();
//            增加默认的创建时间和修改时间
            swiperItem.setCreateTime(date);
            swiperItem.setUpdateTime(date);
            swiperItemRepository.save(swiperItem);
            return "OK";
        }catch (Exception e){
            log.error(String.valueOf(e));
            return "失败";
        }

    }
}
